//
//  MusciPlayView.m
//  27-WeekendHomework
//
//  Created by dllo on 16/8/19.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "MusciPlayView.h"
#import "Masonry.h"
#import <AVFoundation/AVFoundation.h>

@interface MusciPlayView ()
@property (nonatomic, strong) UIButton *packButton;
@property (nonatomic, strong) UIButton *allMusicButton;
@property (nonatomic, strong) UIButton *freeWifiButton;
@property (nonatomic, strong) UIButton *sqlButton;
@property (nonatomic, strong) UIButton *mvButton;
@property (nonatomic, strong) UILabel *authorLabel;
@property (nonatomic, strong) UILabel *changedTime;
@property (nonatomic, strong) UILabel *endTime;
@property (nonatomic, strong) UILabel *songNameLabel;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, strong) UIButton *preButton;
@property (nonatomic, strong) UISlider *slider;
@property (nonatomic, strong) UIImageView *coverImageView;
@property (nonatomic, copy) void (^play)(UIButton *play);
@property (nonatomic, copy) void (^pause)(UIButton *pause);
@property (nonatomic, copy) void (^sliderC)(UISlider *slider);

@end
@implementation MusciPlayView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    _packButton = [self buttonWithImageName:@"miniplayer_btn_playlist_close_b" action:@selector(packButtonDidClicked:)];
    _allMusicButton = [self buttonWithImageName:@"allMusic_more" action:@selector(allMusicButtonDidClicked:)];
   
    _songNameLabel = [self labelWithText:@"阿猫阿狗" color:[UIColor whiteColor]];
    _authorLabel = [self labelWithText:@"陈奕迅" color:[UIColor whiteColor]];

    _freeWifiButton = [self buttonWithImageName:@"player_btn_chinaunicom_normal" action:@selector(freeWifiButtonClicked:)];
    _sqlButton = [self buttonWithImageName:@"player_btn_sq_sel_normal" action:@selector(sqlButtonClicked:)];
    _mvButton = [self buttonWithImageName:@"player_btn_mv_normal" action:@selector(mvButtonClicked:)];

    _coverImageView = [[UIImageView alloc] init];
    _coverImageView.image = [UIImage imageNamed:@"musicCover"];
    _coverImageView.layer.cornerRadius = 125;
    _coverImageView.layer.masksToBounds = YES;
    [self addSubview:_coverImageView];
    
    _slider = [[UISlider alloc] init];
    [_slider setMinimumTrackImage:[UIImage imageNamed:@"player_slider_playback_left"] forState:UIControlStateNormal];
    [_slider setMaximumTrackImage:[UIImage imageNamed:@"player_slider_playback_ritght"] forState:UIControlStateNormal];
    [_slider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"] forState:UIControlStateNormal];
    [_slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_slider];
    
    _changedTime = [self labelWithText:nil color:[UIColor whiteColor]];
    _endTime = [self labelWithText:nil color:[UIColor whiteColor]];

    _pageControl = [[UIPageControl alloc] init];
    _pageControl.numberOfPages = 3;
    [self addSubview:_pageControl];
    
    // 播放相关button
    _playButton = [self buttonWithImageName:@"player_btn_play_normal" action:@selector(playButtonDidClicked:)];
    _preButton = [self buttonWithImageName:@"player_btn_pre_normal" action:@selector(preButtonDidClicked:)];
    _nextButton = [self buttonWithImageName:@"player_btn_next_normal" action:@selector(nextButtonDidClicked:)];
    
    // 底部五个button
    NSArray *normalImages = @[@"player_btn_favorited_normal", @"player_btn_repeat_highlight", @"player_btn_downloaded_normal_1", @"player_btn_share_normal", @"player_btn_playlist_normal"];
    UIButton *lastButton = nil;
    int number = 5;
    CGFloat margin = 10;
    for (int i = 0; i < number; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:normalImages[i]] forState:UIControlStateNormal];
        button.tag = 1001 + i;
        [self addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i % number == 0) {
                make.left.mas_equalTo(0);
            } else {
                make.left.mas_equalTo(lastButton.mas_right).offset(margin);
            }
            if (i % 5 == 4) {
                make.right.mas_equalTo(0);
            }
            make.bottom.mas_equalTo(-margin);
            make.width.mas_equalTo((self.frame.size.width - (number - 1) * margin) / number);
            make.height.mas_equalTo(60);
        }];
        lastButton = button;
    }
}

- (UIButton *)buttonWithImageName:(NSString *)imageName action:(SEL)action {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    return button;
}

- (UILabel *)labelWithText:(NSString *)text color:(UIColor *)color {
    UILabel *label = [[UILabel alloc] init];
    label.text = text;
    label.textColor = color;
    [self addSubview:label];
    return label;
}

- (void)layoutSubviews {
    [_packButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(10);
        make.width.mas_equalTo(50);
        make.width.mas_equalTo(50);
    }];
    
    [_mvButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_sqlButton.mas_right).offset(5);
        make.top.mas_equalTo(_authorLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(_sqlButton);
    }];
    
    [_sqlButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_freeWifiButton.mas_right).offset(5);
        make.top.mas_equalTo(_authorLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(_freeWifiButton);
    }];
    
    [_songNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(25);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(20);
    }];

    [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(_coverImageView.mas_bottom).offset(5);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(20);
    }];
    
    [_authorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(_songNameLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(20);
    }];
    
    [_freeWifiButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0).multipliedBy(0.5f);
        make.top.mas_equalTo(_authorLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(25);
    }];

    [_allMusicButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(_packButton);
        make.width.mas_equalTo(_packButton);
        make.height.mas_equalTo(_packButton);
    }];
    
    [_coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.centerY.mas_equalTo(self).multipliedBy(0.85f);
        make.width.mas_equalTo(250);
        make.height.mas_equalTo(250);
    }];

    [_slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(_coverImageView.mas_bottom).offset(30);
        make.width.mas_equalTo(self).multipliedBy(0.6f);
        make.height.mas_equalTo(20);
    }];

    [_endTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_slider.mas_right).offset(5);
        make.top.mas_equalTo(_changedTime.mas_top);
    }];

    [_changedTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_slider.mas_left).offset(-5);
        make.top.mas_equalTo(_slider);
        make.height.mas_equalTo(20);
    }];

    [_playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.centerY.mas_equalTo(self).multipliedBy(1.6);
        make.width.mas_equalTo(self).multipliedBy(0.2f);
    }];
    
    
    [_preButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_playButton.mas_left).offset(-10);
        make.centerY.mas_equalTo(_playButton.mas_centerY);
        make.width.mas_equalTo(self).multipliedBy(0.18f);
    }];

    [_nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_playButton.mas_right).offset(10);
        make.centerY.mas_equalTo(_preButton.mas_centerY);
        make.width.mas_equalTo(_preButton.mas_width);
    }];

}

- (void)play:(void (^)(UIButton *))play pause:(void (^)(UIButton *))pause {
    self.play = play;
    self.pause = pause;
}

- (void)slider:(void (^)(UISlider *))slider {
    self.sliderC = slider;
}

- (void)playButtonDidClicked:(UIButton *)button {
    [button setImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal];
    [button removeTarget:self action:@selector(playButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(pauseButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.play(button);
}

- (void)pauseButtonDidClicked:(UIButton *)button {
    [button setImage:[UIImage imageNamed:@"player_btn_play_normal"] forState:UIControlStateNormal];
    [button removeTarget:self action:@selector(pauseButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(playButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.pause(button);
}

- (void)nextButtonDidClicked:(UIButton *)nextButton {
    
}

- (void)preButtonDidClicked:(UIButton *)preButton {
    
}

- (void)sliderValueChanged:(UISlider *)slider {
    self.sliderC(slider);
}

- (void)packButtonDidClicked:(UIButton *)packButton {
    
}

- (void)freeWifiButtonClicked:(UIButton *)freeWifiButton {
    
}

- (void)sqlButtonClicked:(UIButton *)sqlButton {
    
}

- (void)allMusicButtonDidClicked:(UIButton *)allMusicButton {
    
}

- (void)dtsButtonClicked:(UIButton *)dtsButton {
    
}

- (void)mvButtonClicked:(UIButton *)mvButton {
    
}

@end
